package com.youxue.controller;

import com.alibaba.fastjson.JSONObject;
import com.cangshi.permission.entity.UserSubject;
import com.youxue.entity.Class;
import com.youxue.entity.*;
import com.youxue.permission.saver.SubjectHolder;
import com.youxue.service.ClassService;
import com.youxue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by 15c on 2018/1/20.
 */
@Controller
@RequestMapping("/class")
public class ClassController {
    @Autowired
    ClassService classService;

    /**
     * 用于分类展示查询一类课程中下所有的具体课程列表
     *
     * @param classTypeId
     * @param page        请求第几页
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/atype", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getATypeAllClass(@RequestParam(required = false) Integer classTypeId,
                            @RequestParam(required = false) Integer page) {
        if (null == classTypeId || null == page) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101, "classTypeId或page参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        //一页的信息条数
        int pageSize = 10;
        //获得总页数
        int allTaskNum = classService.showAClassTypeCount(classTypeId);
        int resultAllPage = allTaskNum / pageSize;
        int resultRemainder = allTaskNum % pageSize;
        if (0 < resultRemainder) {
            resultAllPage = resultAllPage + 1;
        }
        if (0 >= page) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(102, "page参数不可为负");
            return JSONObject.toJSONString(errorJsonObject);
        }
        if (resultAllPage < page) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(103, "请求页数超额");
            return JSONObject.toJSONString(errorJsonObject);
        }
        //从那一条开始请求
        int startPos = pageSize * page - pageSize;
        System.out.println("-----------------" + startPos);
        List<Class> classList = new ArrayList<Class>();
        classList = classService.showATypeAllClassByPage(startPos, pageSize, classTypeId);
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classList);
        correctJsonObject.put("allPage", resultAllPage);
        return JSONObject.toJSONString(correctJsonObject);

    }

    /**
     * 分页关键字搜索课程
     *
     * @param keyWord
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/keyword", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showListClassByKeyWord(@RequestParam(required = false) String keyWord,
                                  @RequestParam(required = false) Integer page) {
        if (null == keyWord || null == page || "".equals(keyWord)) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(105, "keyWord或page参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        //一页的信息条数
        int pageSize = 10;
        //获得总页数
        int allTaskNum = classService.showListClassCountByKeyWord(keyWord);
        int resultAllPage = allTaskNum / pageSize;
        int resultRemainder = allTaskNum % pageSize;
        if (0 < resultRemainder) {
            resultAllPage = resultAllPage + 1;
        }
        if (0 >= page) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(106, "page参数不可为负");
            return JSONObject.toJSONString(errorJsonObject);
        }
        if (resultAllPage < page) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(107, "请求页数超额，或无该关键字搜索");
            return JSONObject.toJSONString(errorJsonObject);
        }
        int startPos = pageSize * page - pageSize;
        System.out.println("-----------------" + startPos);
        List<Class> classList = new ArrayList<Class>();
        classList = classService.showListClassByKeyWord(keyWord, startPos, pageSize);
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classList);
        correctJsonObject.put("allPage", resultAllPage);
        return JSONObject.toJSONString(correctJsonObject);

    }

    /**
     * 用于展示查询一们课程中下所有的具体课程信息（标题、简介、适应人群、价格.....）
     *
     * @param classId 课程id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/aclassinfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getAClassInfo(@RequestParam(required = false) Integer classId) {
        if (null == classId) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(109, "classId参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        Class cource = new Class();
        cource = classService.showAClassInfo(classId);
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(cource);
        return JSONObject.toJSONString(correctJsonObject);
    }

    /**
     * 用于显示学习人数最多的课程
     *
     * @param num 限制显示个数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/classmore", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showListClassCountMore(@RequestParam(required = false) Integer num) {
        if (null == num || num == 0) {
            num = 5;
        }
        List<Class> classList = classService.showListClassCountMore(num);

        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classList);
        return JSONObject.toJSONString(correctJsonObject);

    }

    /**
     * 用于分页显示学习人数最多的课程
     */
@ResponseBody
@RequestMapping(value = "/classmoststudy", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showMostStudyClass(@RequestParam(required = false) Integer page){

    if (null == page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(105, "page参数为空");
        return JSONObject.toJSONString(errorJsonObject);
    }
    //一页的信息条数
    int pageSize = 10;
    //获得总页数
    int allTaskNum = classService.selectMostStudyClassNum();
    int resultAllPage = allTaskNum / pageSize;
    int resultRemainder = allTaskNum % pageSize;
    if (0 < resultRemainder) {
        resultAllPage = resultAllPage + 1;
    }
    if (0 >= page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(106, "page参数不可为负");
        return JSONObject.toJSONString(errorJsonObject);
    }
    if (resultAllPage < page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(107, "请求页数超额，或无该关键字搜索");
        return JSONObject.toJSONString(errorJsonObject);
    }
    int startPos = pageSize * page - pageSize;
    System.out.println("-----------------" + startPos);
    List<Class> classList = new ArrayList<Class>();
    classList = classService.selectMostStudyClassByPage( startPos, pageSize);
    CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
    correctJsonObject.setData(classList);
    correctJsonObject.put("allPage", resultAllPage);
    return JSONObject.toJSONString(correctJsonObject);
}  /**
     * 用于分页显示最新课程
     */
@ResponseBody
@RequestMapping(value = "/classlatest", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showLatestStudyClass(@RequestParam(required = false) Integer page){

    if (null == page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(105, "page参数为空");
        return JSONObject.toJSONString(errorJsonObject);
    }
    //一页的信息条数
    int pageSize = 10;
    //获得总页数
    int allTaskNum = classService.selectMostStudyClassNum();
    int resultAllPage = allTaskNum / pageSize;
    int resultRemainder = allTaskNum % pageSize;
    if (0 < resultRemainder) {
        resultAllPage = resultAllPage + 1;
    }
    if (0 >= page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(106, "page参数不可为负");
        return JSONObject.toJSONString(errorJsonObject);
    }
    if (resultAllPage < page) {
        ErrorJsonObject errorJsonObject = new ErrorJsonObject(107, "请求页数超额，或无该关键字搜索");
        return JSONObject.toJSONString(errorJsonObject);
    }
    int startPos = pageSize * page - pageSize;
    System.out.println("-----------------" + startPos);
    List<Class> classList = new ArrayList<Class>();
    classList = classService.selectOrderClassTimeByPage( startPos, pageSize);
    CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
    correctJsonObject.setData(classList);
    correctJsonObject.put("allPage", resultAllPage);
    return JSONObject.toJSONString(correctJsonObject);
}
    /**
     * 按课程发布时间降序排列筛选
     *
     * @param num 限制显示个数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/classtime", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showListClassByTime(@RequestParam(required = false) Integer num) {
        if (null == num || num == 0) {
            num = 5;
        }
        List<Class> classList = classService.showListClassByTime(num);

        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(classList);
        return JSONObject.toJSONString(correctJsonObject);

    }

    @Autowired
    UserService userService;

    /**
     * 推荐课程
     *
     * @param num
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/recommendation", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showListRecommendationClass(@RequestParam(required = false) Integer num) {
        if (null == num || num == 0) {
            num = 5;
        }
        try {
            // 获取当前访问对象
            UserSubject subject = (UserSubject) SubjectHolder.getSubject();

            //获取当前的登录信息
            UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
            //获取当前的用户登录id
            int userId = userEntity.getUserId();
            User user = userService.showUserPartInfo(userId);
            if (user.getUserBirthday()!=null){

                //获取当前年月日
                Calendar cal = Calendar.getInstance();
                System.out.println("--------------------------"+cal);
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH )+1;

                System.out.println("--------------------------"+year + " 年 " + month + " 月");
            }
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(userId);
            return JSONObject.toJSONString(correctJsonObject);
        } catch (NullPointerException e) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("id为null");
            return JSONObject.toJSONString(correctJsonObject);
        }
    }
}
